package projetoeCommerce.Dados;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;


import projetoeCommerce.Entidades.Usuario;

public class DadosUsuario {



	public void Inserir(Usuario u) throws SQLException
	{
		Connection conexao = Conexao.GetConexao();
		try {

			Statement stm = conexao.createStatement();

			StringBuilder sb = new StringBuilder("Insert into Usuarios " +
			"(Nome )");

			sb.append("values (");
			sb.append("'" + u.Nome + "', ");
			sb.append(") ");

			System.out.println(sb.toString());
			stm.execute(sb.toString());
		}
		finally
		{
			conexao.close();
		}
	}

	public void Alterar(Usuario u) throws SQLException
	{
		Connection conexao = Conexao.GetConexao();
		try {

			Statement stm = conexao.createStatement();

			StringBuilder sb = new StringBuilder("Update Usuarios SET ");
			sb.append("Nome = '" + u.Nome + "', ");
			sb.append("WHERE CodUsuario = " + u.get_codigo());

			System.out.println(sb.toString());
			stm.execute(sb.toString());
		}
		finally
		{
			conexao.close();
		}
	}

	public void Remover(Usuario u) throws SQLException
	{
		Remover(u.get_codigo());
	}

	public void Remover(int codigo) throws SQLException
	{
		Connection conexao = Conexao.GetConexao();
		try {

			Statement stm = conexao.createStatement();

			StringBuilder sb = new StringBuilder("Delete from Usuarios ");
			sb.append("WHERE CodUsuario = " + codigo);

			System.out.println(sb.toString());
			stm.execute(sb.toString());
		}
		finally
		{
			conexao.close();
		}
	}

	public Usuario GetPorCodigo(int codigo) throws SQLException
	{
		Usuario u = null;

		Connection conexao = Conexao.GetConexao();
		try {

			Statement stm = conexao.createStatement();

			StringBuilder sb = new StringBuilder("Select * from Usuarios ");
			sb.append("WHERE CodUsuario = " + codigo);

			System.out.println(sb.toString());
			ResultSet rs = stm.executeQuery(sb.toString());

			if (rs.next()) {
				u = ControiUsuarioComORS(rs); 
			}

			rs.close();
			stm.close();

		}
		finally
		{
			conexao.close();
		}
		return u;
	}

	public HashMap<String, Usuario> getTodos() throws SQLException{

		HashMap<String, Usuario> usuarios = new HashMap<String, Usuario>();

		Connection conexao = Conexao.GetConexao();
		try {

			Statement stm = conexao.createStatement();

			StringBuilder sb = new StringBuilder("Select * from Usuarios order by CodUsuario asc");

			System.out.println(sb.toString());
			ResultSet rs = stm.executeQuery(sb.toString());

			while (rs.next()) {
				Usuario u = ControiUsuarioComORS(rs);
				usuarios.put(u.get_codigo() + "", u);
			}

			rs.close();
			stm.close();

		}
		finally
		{
			conexao.close();
		}

		return usuarios;
	}

	private Usuario ControiUsuarioComORS(ResultSet rs) throws SQLException {
		Usuario c = new Usuario();

		c.set_codigo(rs.getInt("CodUsuario")); 
		c.Nome = rs.getString("Nome");
		return c;
	}



}
